﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using NRemedy;
using NRemedy.Linq;

namespace CMDBImportCIDLL
{
    public class CommonDTOperate<T> where T : ASTBase
    {

        static public void CreateAndUpdate(string server, string login, string password, Company company, List<T> list, ShowMessageDelegate show)
        {
            using (ARLoginContext context = new ARLoginContext(server, login, password))
            {
                ARProxy<T> proxy = new ARProxy<T>(context);
                foreach (T item in list)
                {
                    string meaasge = string.Empty;
                    item.Submitter = login;
                    item.Short_Description = item.Name;
                    item.DataSetId = DatasetId.BMCASSET;
                    item.AssigneeGroupPermissions = string.Concat(";", Permission.PUBLIC, ";", company.CompanyPermission, ";");
                    item.Company = company.CompanyName;
                    //item.AssetLifecycleStatus = ASTBase.AssetLifecycleStatus_Enum.Deployed;//3
                    item.Status = ARRegularForm.Status_Enum.Rejected;//3

                    T t = (new CommonDTOperate<T>()).checkCiNameExist(context, item);
                    if (t == null)
                    {
                        string requestId = proxy.CreateEntry(item);
                        meaasge = string.Concat("插入数据：", (new CommonDTOperate<T>()).getItemStr(item), "|RequestId:", requestId);
                    }
                    else
                    {
                        PropertyInfo[] properties = t.GetType().GetProperties();
                        PropertyInfo[] noproperties = typeof(ARRegularForm).GetProperties();
                        List<string> nopiName = new List<string>();
                        foreach (var i in noproperties)
                        {
                            nopiName.Add(i.Name);
                        }
                        foreach (var pi in properties)
                        {
                            if (!nopiName.Contains(pi.Name) || pi.Name == "Status")
                            {
                                PropertyInfo pv = item.GetType().GetProperty(pi.Name);
                                pi.SetValue(t, pv.GetValue(item, null), null);
                            }
                        }
                        proxy.SetEntry(t);
                        meaasge = string.Concat("更新数据：", (new CommonDTOperate<T>()).getItemStr(t), "|RequestId:", t.Request_ID);
                    }
                    show(meaasge);
                }
            }
        }

        static public void CreateSkipUpdate(string server, string login, string password, Company company, List<T> list, ShowMessageDelegate show)
        {
            using (ARLoginContext context = new ARLoginContext(server, login, password))
            {
                ARProxy<T> proxy = new ARProxy<T>(context);
                foreach (T item in list)
                {
                    string requestId = string.Empty;
                    item.Submitter = login;
                    item.Short_Description = item.Name;
                    item.DataSetId = DatasetId.BMCASSET;
                    item.AssigneeGroupPermissions = string.Concat(";", Permission.PUBLIC, ";", company.CompanyPermission, ";");
                    item.Company = company.CompanyName;
                    //item.AssetLifecycleStatus = ASTBase.AssetLifecycleStatus_Enum.Deployed;//3
                    item.Status = ARRegularForm.Status_Enum.Rejected;//3
                    T t = (new CommonDTOperate<T>()).checkCiNameExist(context, item);
                    if (t == null)
                    {
                        requestId = proxy.CreateEntry(item);
                        string meaasge = string.Concat("插入数据：", (new CommonDTOperate<T>()).getItemStr(item), "|RequestId:", requestId);
                        show(meaasge);
                    }
                  
                }
            }
        }

        static public void Delete(string server, string login, string password, Company company, List<T> list, ShowMessageDelegate show)
        {
            using (ARLoginContext context = new ARLoginContext(server, login, password))
            {
                ARProxy<T> proxy = new ARProxy<T>(context);
                foreach (T item in list)
                {
                    string meaasge = string.Empty;
                    item.Submitter = login;
                    item.Short_Description = item.Name;
                    item.DataSetId = DatasetId.BMCASSET;
                    item.AssigneeGroupPermissions = string.Concat(";", Permission.PUBLIC, ";", company.CompanyPermission, ";");
                    item.Company = company.CompanyName;
                    //item.AssetLifecycleStatus = ASTBase.AssetLifecycleStatus_Enum.Deployed;
                    T t = (new CommonDTOperate<T>()).checkCiNameExistOfDelete(context, item);
                    if (t != null)
                    {
                        proxy.DeleteEntry(t);
                        meaasge = string.Concat("删除数据：", (new CommonDTOperate<T>()).getItemStr(t), "|RequestId:", t.Request_ID);
                        show(meaasge);
                    }
                   
                }
            }
        }

        private T checkCiNameExist(ARLoginContext context, T item)
        {
            ARSet<T> aa = new ARSet<T>(context, new ARServerDefaultFactory());
            var q = from s in aa
                    where (s.DataSetId == DatasetId.BMCASSET) && (s.Category == item.Category) && (s.Type == item.Type) && (s.Item == item.Item) && (s.Company == item.Company) && (s.Name == item.Name)
                    select s;
            return q.ToList().FirstOrDefault();

        }

        private T checkCiNameExistOfDelete(ARLoginContext context, T item)
        {
            ARSet<T> aa = new ARSet<T>(context, new ARServerDefaultFactory());
            var q = from s in aa
                    where (s.DataSetId == DatasetId.BMCASSET) && (s.Name == item.Name)
                    select s;
            return q.ToList().FirstOrDefault();

        }

        private string getItemStr(T t)
        {
            string str = string.Empty;
            PropertyInfo pi = t.GetType().GetProperty("Item");
            if (pi != null)
            {
                str = string.Concat(str, "|", pi.Name, ":", pi.GetValue(t, null));
            }
            PropertyInfo pi2 = t.GetType().GetProperty("Name");
            if (pi2 != null)
            {
                str = string.Concat(str, "|", pi2.Name, ":", pi2.GetValue(t, null));
            }
            return str;
        }
    }
}
